package com.frame.pageform;

import java.io.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.frame.pageform.component.JsonObject;


abstract public class PageForm implements Serializable{

	private static Map forwardMap=new HashMap();
	private JsonObject jsonObject;

	static public void initForward(String path){
		String className = "";
		Throwable th = new Throwable();
		StackTraceElement[] stes = th.getStackTrace();
		for(int i = 1; true; i++)
		{
			className = stes[i].getClassName();
			if(!className.equals(PageForm.class.getName()))
				break;
		}
		FileInputStream is=null;
		try {
			path=RequestContext.getContext().getRealPath("/")+path;
			is = new FileInputStream(path);

			Properties dbProps = new Properties();
			dbProps.load(is);
			Enumeration propNames = dbProps.propertyNames();
			while (propNames.hasMoreElements())
			{
				String name = (String) propNames.nextElement();
				String url=dbProps.getProperty(name);
				try
				{
					Class c = Class.forName(className);
					setForward(c,name,url);
				}
				catch(ClassNotFoundException ex)
				{
					ex.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try{
				is.close();
			}catch(Exception e){}
		}
	}
	static protected void setForward(String name,String path)
	{
		String className = "";
		Throwable th = new Throwable();
		StackTraceElement[] stes = th.getStackTrace();
		for(int i = 1; true; i++)
		{
			className = stes[i].getClassName();
			if(!className.equals(PageForm.class.getName()))
				break;
		}

		try
		{
			Class c = Class.forName(className);

			setForward(c,name,path);
		}
		catch(ClassNotFoundException ex)
		{
			ex.printStackTrace();
		}
	}
	static protected void setForward(String path)
	{
		setForward("",path);
	}
	/**
	 * 设置页面路径
	 * @param c
	 * @param name
	 * @param path
	 */
	static public void setForward(Class c,String name,String path)
	{
		if(path == null)
			path = "";

		if(c == null)
			c = PageForm.class;

		Map pageMap = null;
		synchronized(forwardMap)
		{
			pageMap = (Map)forwardMap.get(c);
			if(pageMap == null)
			{
				pageMap = new Hashtable();
				forwardMap.put(c,pageMap);
			}
		}

		pageMap.put(name,path);
	}
	public String forward(String name){
		return getForward(name);
	}
	public String forward(){
		String s=getForward("");
		return s;
	}
	public String getForward(String name){
		if(name == null)
			name = "";
		synchronized(forwardMap)
		{
			String result = null;
			Class c = getClass();

//	         while(c != Object.class)
//	         {
			Map pageMap = (Map)forwardMap.get(c);
			if(pageMap != null)
				result = (String)pageMap.get(name);

			if(result != null)
				return result;
//	         }
		}
		return "null";
	}
	abstract public String execute() throws Exception;

	public HttpServletRequest getRequest(){
		return RequestContext.getContext().getRequest();
	}
	public HttpServletResponse getResponse(){
		return RequestContext.getContext().getResponse();
	}
	public String getErrorRepath(boolean lsajax,Exception error){
		if(lsajax)
			return "/platform/MessageJson.jsp";
		else
			return "/platform/Message.jsp";
	}

	public static Map getForwardMap() {
		return forwardMap;
	}
	public void init(){

	}
	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public JsonObject getJsonObject() {
		if(jsonObject==null)
			jsonObject=new JsonObject();
		return jsonObject;
	}

}
